const app = getApp()
function request(url, params, success, fail, method) {
  if (app.data.sessionId == '' || app.data.sessionId == null){
    wx.navigateTo({
      url: '/pages/loginAgain/loginAgain'
    })
  }else{
    this.requestLoading(url, params, "加载中", success, fail, method)
  }
}
// 展示进度条的网络请求
// url:网络请求的url
// params:请求参数
// message:进度条的提示信息
// success:成功的回调函数
// fail：失败的回调
function requestLoading(url, params, message, success, fail, method) {
  wx.showLoading({
    title: message,
  })
  wx.request({
    url: app.data.serverIp + app.data.serverPort + url,
    data: params,
    header: {
      //'content-type': 'application/x-www-form-urlencoded'
      'Content-Type': 'application/json',
      'cookie': app.data.sessionId
    },
    method: method,
    success: function (res) {
      wx.hideNavigationBarLoading()
      wx.hideLoading()
      if (res.statusCode == 200) {
        if (res.data.status == 0){
          success(res.data)
        }else if (res.data.status == 10){
          wx.navigateTo({
            url: '/pages/loginAgain/loginAgain'
          })
        }else{
          fail()
        }
      } else {
        fail()
      }
    },
    fail: function (res) {
      wx.hideLoading()
      fail()
    },
    complete: function (res) {

    },
  })
}
module.exports = {
  request: request,
  requestLoading: requestLoading
}